//
//  DFWelcomeViewController.swift
//  新浪微博
//
//  Created by dafei on 16/5/23.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class DFWelcomeViewController: UIViewController {
    
    override func loadView() {
        
        view = backView
        
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    
    }

    
    //设置UI界面
    private func setupUI() {
    
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        //设置约束
        //在初始化就是设置约束  不要在viewdidlayoutSubviews中设置约束
        //系统是根据提前制定好的约束 在viewdidlayoutSubviews 方法中根据约束修改frame
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.size.equalTo(CGSizeMake(90, 90))
        }
    
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        iconView.sd_setImageWithURL(DFUserAccountViewModel.sharedAccountViewModel.headURL)
        //切圆角
        iconView.cornerRadius = 45
        iconView.layer.masksToBounds = true

        nameLabel.alpha = 0
   
    }
    
    //动画效果放在viewdidApper阶段
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    
    private func startAnimation() {
        //弹簧动画效果
        let offset = -ScreenHeight + 160
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: { 
            //更新约束
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
            })
            //提前刷新父视图
            self.view.layoutIfNeeded()
            
            }) { (_) in
                UIView.animateWithDuration(0.5, animations: { 
                    self.nameLabel.alpha = 1
                    }, completion: { (_) in
                        
                        print("👌")
                     
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
                        
                })
                
            }
 
        }
    
    override func prefersStatusBarHidden() -> Bool{
        
        return true
        
    }
 
    
    //懒加载子视图
    //背景视图
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //用户名称文章
//    private lazy var nameLabel:UILabel = UILabel(title: DFUserAccountViewModel.sharedAccountViewModel.userAccount?.name ?? "", font: 18, textColor: UIColor.darkGrayColor())
//    
    
    
    private lazy var nameLabel: UILabel = {
    
        let l = UILabel()
        print(DFUserAccountViewModel.sharedAccountViewModel.userAccount?.name)
        
        l.text = DFUserAccountViewModel.sharedAccountViewModel.userAccount?.name
//        l.text = "wwww"
        l.font = UIFont.systemFontOfSize(18)
        l.textColor = UIColor.darkGrayColor()
        l.alpha = 0
        l.sizeToFit()
  
      return l
    }()
    
   
  
}
